#!/usr/bin/perl -w

use Tk;
use Tk::ROText;
use VAISFunc;
use PrlServer::API qw( $API_release );

my $blob = 0x000;
my $HCS_VERSION = "J13";

my($SPAIG) = PrlServer::API->new (
  host => "VAIS[SPAIG]://mcs/HCS",
  proto => 'tcp',
  ip => '127.0.0.1',
  port => 18359
);

my($HCSServer) = PrlServer::API->new (
  host => "mcs/HCS internal server",
  proto => 'tcp',
  ip => '127.0.0.1',
  port => 18358
);

my($HCSClient) = PrlServer::API->new (
  host => "mcs/HCS internal client",
  proto => 'tcp',
  ip => '127.0.0.1',
  port => 18358
);

$HCSServer->createServer ();


my $win = MainWindow->new;

$win->configure (-title => 'mcs/HCS['.$HCS_VERSION.'] Workstation', -background => 'black');
$win->geometry ('1400x880+2+26');

my $mmb = $win->Frame (-borderwidth => 1,
  -borderwidth => 1,
  -background => 'black',
  -foreground => 'green')->pack (-fill => 'x',
    -side => 'top',
    -anchor => 'n',
    -expand => 1);

my $mmb_view = $mmb->Menubutton (-text => "Viewer",
  -relief => 'groove',
  -activebackground => 'black',
  -activeforeground => 'blue',
  -background => 'black',
  -font => "courier 12 bold",
  -foreground => 'green')->pack (-side => 'left');

$mmb_view->command (-label => 'Maximize',
  -activebackground => 'black',
  -activeforeground => 'blue',
  -font => "courier 12 bold",
  -command => sub{ $win->geometry ('1400x880+50+50') });
  
$mmb_view->command (-label => 'Minimize',
  -activebackground => 'black',
  -activeforeground => 'blue',
  -font => "courier 12 bold",
  -command => sub{ $win->geometry ('700x440+100+300') });
  


my $middle = $win->Frame (-borderwidth => 1,
  -borderwidth => 1,
  -background => 'black',
  -foreground => 'yellow')->pack (-fill => 'x',
    -side => 'top', -anchor => 'nw', -expand => 1);

my $bottom = $win->Frame (-borderwidth => 1,
  -borderwidth => 1,
  -background => 'black',
  -foreground => 'green')->pack (-fill => 'x',
    -side => 'bottom',
    -anchor => 'sw');
my $m = "mcs/HCS[$HCS_VERSION] Workstation:\n";
my $msg = $middle->Scrolled ('ROText',
  -background => 'black',
  -foreground => 'yellow',
  -font => "courier 12 bold",
  -borderwidth => 0,
  -relief => "groove",
  -scrollbars => 'oe')->pack (-side => 'left', -anchor => 'nw', -expand => 1, -fill => "both");
$msg->Contents ("mcs/HCS[$HCS_VERSION] Workstation:\n");
my $text = ">: ";
my $txt = $bottom->Label (-textvariable => \$text,
  -background => 'black',
  -foreground => 'red',
  -font => "courier 12 bold",
  -justify => 'left')->pack (-side => 'left', -anchor => 'w');
  
$win->bind ( '<Any-KeyPress>' => sub
    {
        my($c) = @_;
        my $e = $c->XEvent;
        my( $x, $y, $W, $K, $A, $N ) = ( $e->x, $e->y, $e->K, $e->W, $e->A, $e->N);
        if($N == 65293){ #return key
          print "ret";
          my $line_in = substr $text, 3;
          $text = ">: ";
          my @lines = split (/ /, $line_in);
          my $cmd = $lines [0];
          if($cmd eq "exit"){
            exit;
          }elsif($cmd eq "help"){
            $m .= "Help ::\n".
              "  exit -- terminates this process\n".
              "  help -- displays this help\n".
              "  clearscreen -- clears the screen\n".
              "  VAIS -- {\n".
              "    dump [data] -- dumps data to VAIS\n".
              "    ||AIG -- gets the AIG handler's name\n".
              "    halt -- stops VAIS\n".
              "    ||VAIS_RELEASE -- Gets the version of VAIS on this workstation\n".
              "    --get-release -- Same as ||VAIS_RELEASE\n".
              "    version -- Same as ||VAIS_RELEASE\n".
              "  VERSION -- display versioning\n";
          }elsif($cmd eq "clearscreen"){
            $m = "mcs/HCS[$HCS_VERSION] Workstation:\n";
          }elsif($cmd eq "VAIS"){
            if($lines [1] eq "dump"){
              VAISFunc::__VAIS_dump ($HCSClient, $SPAIG, $line_in);
            }elsif($lines [1] eq "||AIG"){
              $m .= "Asking for AIG handle...\n";
              my $AIG = VAISFunc::__VAIS_getAIGHandle ($HCSClient, $SPAIG);
              $m .= "||AIG -> $AIG\n";
            }elsif($lines [1] eq "halt"){
              $m .= "Sending HALT interrupt (0x90 ? 0x003)...\n";
              VAISFunc::__VAIS_halt ($HCSClient, $SPAIG);
            }else{
              $m .= "$lines[1] .. not recognized\n";
              $HCSClient->clientPortOut (" . ");
            }
            $m .= "||HCSServer [";
            for(my $n = $HCSServer->serverPortIn (); $n != 0; $n--){
              $m .= " ".$HCSServer->serverPortIn ()." ";
            }
            $m .= "]\n";
          }elsif($cmd eq "VERSION"){
            $m .= "mcs/HCS Copyright Matthew Caswell, 2013\n".
              "||HCS_VERSION -> $HCS_VERSION\n".
              "H.C.S. -- Hub Control System\n";
          }
          else{
            $m .= "$cmd -: Unknown command!\n";
          }
          
          $msg->Contents ($m);
          $msg->see ('end');
        }elsif($N == 65288){ #backspace
          chop ($text);
        }else{
          $text .= $A;
        }
        print "$text\n";
        print "A key was pressed:\n";
        print "  x = $x\n";
        print "  y = $y\n";
        print "  W = $K\n";
        print "  K = $W\n";
        print "  A = $A\n";
        print "  N = $N\n";
    } );

MainLoop;
